#include "sprite.h"
#include "sdl_helper.h"

struct dd_sprite *dd_sprite_create() {
	struct dd_sprite *w = malloc(sizeof(struct dd_sprite));
	dd_sprite_init(w);
	return w;
}

void dd_sprite_load(struct dd_sprite *this, const char *name) {
	this->texture = dd_load_image(name);
}

void dd_sprite_draw(struct dd_sprite *this) {
	this->rect.x = this->x;
	this->rect.y = this->y;
	this->rect.w = this->w;
	this->rect.h = this->h;
	SDL_RenderCopy(dd_ren, this->texture, NULL, &this->rect);
}

void dd_sprite_init(struct dd_sprite *this) {
	this->load = dd_sprite_load;
	this->draw = dd_sprite_draw;
	this->collide = dd_sprite_collide;
	this->rect.x = this->x = 0;
	this->rect.y = this->y = 0;
	this->rect.w = this->w = 100;
	this->rect.h = this->h = 100;
}

int dd_sprite_collide(struct dd_sprite *a, struct dd_sprite *b) {
	return (
		// collision on X
		(a->rect.x < (b->rect.x +b->rect.w)) !=
		((a->rect.x +a->rect.w) <= b->rect.x) &&
		// collision on Y
		(a->rect.y < (b->rect.y +b->rect.h)) !=
		((a->rect.y +a->rect.h) <= b->rect.y)
	);
}
